<?php



class Login extends Controller {



    function Login() {

        parent::Controller();    

    }

    

    function index() {
        $this->session->sess_destroy();
        $this->load->view('login');

    }



    function masuk($nama_user, $passwd) {

        $this->load->model('Login_model', 'l');

        $this->load->model('Datum_model');

        $id = Datum_model::getData('id_pegawai','user',"namauser = '$nama_user'");

        $nip = Datum_model::getData('nip','pegawai',"idspg_pegawai = '$id'");
        
        $id_akses = Datum_model::getData('id_akses','user',"namauser = '$nama_user'");

        if ($this->l->isValidLogin($nama_user, $passwd)) {

            $data = array(

                'username' => $nama_user,

                'role' => $this->l->getRole($nama_user),

                'kabkota' => KABKOTA,

                'nip' => $nip,
                'id_akses' => $id_akses

            );

            $this->session->set_userdata($data);

			$this->db->where('namauser',$nama_user);
			$this->db->update('user',array('session' =>  $this->session->userdata('session_id'), 'time' => time()));

            //redirect("welcome", "refresh");

            echo "sukses";

        } else {

            //redirect("login", "refresh");

            echo "not-sukses";

        }

    }

    

    function keluar() {

        $this->session->sess_destroy();

        redirect("login", "refresh");

    }

    

    function register() {

        $this->load->model('Login_model', 'l');

        $this->load->model('Datum_model');

        

        $nip = $this->input->post('nip');

        $namauser = $this->input->post('namauser');

        $passwd = $this->input->post('passwd');

        $true = Datum_model::getDataAll('pegawai','CONCAT(nip,nip_baru)="'.$nip.'"');        

        if ( $true != false) {

            

            $arr['namauser'] = $namauser;            

            $arr['nama_asli'] = $true['nama'];

            $arr['passwd'] = md5($passwd);

            $arr['id_grup'] = '0';  

            $arr['id_pegawai '] = $true['idspg_pegawai'];  

            if($this->db->insert('user', $arr)){

                $data = array(

                'username' => $namauser,

                'role' => '0',

                'nip' => $nip

            );

            $this->session->set_userdata($data);

                echo "sukses";

            }

        } else {

            //redirect("login", "refresh");

            echo "not-sukses";

        }

    }



}



/* End of file login.php */

/* Location: ./system/application/controllers/login.php */